import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:hxdj/common/components/my_picture_preview.dart';
import 'package:hxdj/common/components/my_webview.dart';
import 'package:hxdj/common/routers/base_routers.dart';
import 'package:hxdj/home/address/edit_address/edit_address_page.dart';
import 'package:hxdj/home/address/loca_address/loca_address_page.dart';
import 'package:hxdj/home/address/select_address/select_address_page.dart';
import 'package:hxdj/home/order/order_create_page.dart';
import 'package:hxdj/home/order/order_page.dart';
import 'package:hxdj/home/project_details/project_details_page.dart';
import 'package:hxdj/home/technician_detail.dart/technician_detail_page.dart';
import 'package:hxdj/recommend_project/recommend_project_page.dart';
import 'package:hxdj/home/recommend_technician/recommend_technician_page.dart';
import 'package:hxdj/home/select_technician/select_technician_page.dart';
import 'package:hxdj/message/rongcloud/rc_chat/rc_chat_page.dart';
import 'package:hxdj/message/sys_message/sys_message_page.dart';
import 'package:hxdj/mine/about_mine_page.dart';
import 'package:hxdj/mine/cancel_account/cancel_account_page.dart';
import 'package:hxdj/mine/member_center/member_center_page.dart';
import 'package:hxdj/mine/questions/questions_page.dart';
import 'package:hxdj/mine/safe_account/safe_account_page.dart';
import 'package:hxdj/home/order/order_detail/order_detail_page.dart';
import 'package:hxdj/splash.dart';
import 'package:hxdj/tupeng/tupeng_text.dart';

class PageRouterManager implements IRouterProvider {
  @override
  void initRouter(FluroRouter router) {
    //主页面

    //关于我们
    router.define(
      MyMainPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const MyMainPage(
          title: '',
        );
      }),
    );

    //关于我们
    router.define(
      AboutMinePage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const AboutMinePage();
      }),
    );

    //注销账号
    router.define(
      CancelAccountPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const CancelAccountPage();
      }),
    );

    //账号安全
    router.define(
      SafeAccountPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const SafeAccountPage();
      }),
    );

    // 常见问题
    router.define(
      QuestionsPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const QuestionsPage();
      }),
    );

    //会员中心
    router.define(
      MemberCenterPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const MemberCenterPage();
      }),
    );
    //套餐详情

    router.define(
      PackageDetailPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String? gId = parameters['gId']?.first; // 记录入口
        final String? tId = parameters['tId']?.first; // 记录入口
        return PackageDetailPage(tId: tId ?? "", gId: gId ?? "");
      }),
    );
    //选择技师
    router.define(
      SelectTechnicianPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String? tId = parameters['tId']?.first; //
        return SelectTechnicianPage(templateId: tId ?? "");
      }),
    );

    //系统消息
    router.define(
      SysMessagePage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String messageId = parameters['messageId']?.first; // 记录入口
        return SysMessagePage(messageId: messageId);
      }),
    );

    //聊天页面
    router.define(
      RCChatPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String targetId = parameters['targetId']?.first; // 记录入口
        return RCChatPage(targetId: targetId);
      }),
    );

    //选择地址
    router.define(
      SelectAddressPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String form = parameters['form']?.first; // 记录入口
        return SelectAddressPage(form: form);
      }),
    );

    //编辑地址
    router.define(
      EditAddressPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String? addressId = parameters['addressId']?.first ?? ""; // 记录入口
        return EditAddressPage(addressId: addressId ?? "");
      }),
    );

    //定位地址
    router.define(
      LocaAddressPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const LocaAddressPage();
      }),
    );

    //推荐技师

    router.define(
      RecommendTechnicianPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const RecommendTechnicianPage();
      }),
    );

    //webview的页面
    router.define(
      MyWebviewPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String title = parameters['title']?.first; // 记录入口
        final String url = parameters['url']?.first; // 记录入口
        return MyWebviewPage(title: title, url: url);
      }),
    );

    //订单详情
    router.define(
      OrderDetailPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String orderId = parameters['orderId']?.first; // 记录入口

        return OrderDetailPage(orderId: orderId);
      }),
    );

    //推荐项目
    router.define(
      RecommendProjectPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const RecommendProjectPage();
      }),
    );

    //订单页面
    router.define(
      OrderPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const OrderPage();
      }),
    );

    //技师详情

    router.define(
      TechnicianDetailPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String tId = parameters['tId']?.first; // 技师ID
        return TechnicianDetailPage(tId: tId);
      }),
    );

    //创建订单
    router.define(
      OrderCreatePage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const OrderCreatePage();
      }),
    );

    //预览图片
    router.define(
      MyPicturePreviewPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        final String? index = parameters['index']?.first; //
        final String? heroTag = parameters['heroTag']?.first; //
        Map<String, dynamic> arguments = {
          "currentPage": index,
          "heroTag": heroTag ?? "",
        };

        if (parameters['imgUrls'] != null && parameters['imgUrls'].isNotEmpty) {
          final List<dynamic> imgUrls = List.from(jsonDecode(parameters['imgUrls']?.first));
          arguments.addAll({
            "imageUrls": imgUrls,
          });
        }

        if (parameters['imageBase64s'] != null && parameters['imageBase64s'].isNotEmpty) {
          final List<dynamic> imageBase64s = jsonDecode(parameters['imageBase64s']?.first); //
          arguments.addAll({
            "imageBase64s": imageBase64s,
          });
        }

        return MyPicturePreviewPage(
          arguments: arguments,
        );
      }),
    );
    //测试页面
    router.define(
      TupengPage.routeName,
      handler: Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
        return const TupengPage();
      }),
    );
  }
}
